global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/interest_rates.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {

******interest rates: data source******
****IMF for countries that are missing in the OECD data from Geerolf
*IMF International Financial Statistics (IFS), "Interest Rates selected indicators", Accessed April 19, 2021, https://data.imf.org/?sk=4c514d48-b6ba-49ed-8ab9-52b0c1a0179b&sid=1390030341854
import excel ${mow_data_raw}/interest_rates/Interest_Rates.xlsx, sheet("Interest Rates") cellrange(A7:EY80) firstrow clear
keep Country E F G H I J K L M N O P Q R S
ren Country country
keep if country =="Greece" | country =="Korea, Rep. of" |country =="Luxembourg"
replace country ="KOR" if country == "Korea, Rep. of"
replace country ="GRC" if country == "Greece"
replace country ="LUX" if country == "Luxembourg"

*rename columns
forvalues year=1995/2009 {
	local i = `year' - (1995-69)
	local x = char(`i')
	gen L_intr`year' = real(`x')
	drop `x'
}

tempfile imf
save `imf', replace

*OECD data, main source for this
global countrylist_Lintr "AT AU BE CA CH DE DK ES FI FR GB GR IE IT JP KR LU NL PT SE US"
*Francois Geerolf, "Monthly Monetary and Financial Statistics (MEI)", Accessed March 12, 2021. https://fgeerolf.com/data/oecd/MEI_FIN.html
import delimited ${mow_data_raw}/interest_rates/MEI_FIN_12032021153020223.csv, varnames(1) encoding (UTF-8) clear

*keep only annual long term interest rate
keep if subject == "IRLT"
keep if frequency=="A"
keep location time value
ren time year
destring year, replace
keep if year >=1995 & year<=2009
ren location country
ren value L_intr
reshape wide L_intr, i(country) j(year)
drop if country =="KOR" | country =="GRC" |country =="LUX"
append using `imf'

tempfile int
save `int'

*import iso alpa 2 to iso alpha 3 to make the data consistent
import excel ${mow_data_raw}/countries/ctry_code_2v3.xlsx, sheet("Tabelle1") firstrow clear
ren digitctrycode digit_2ctry
ren B digit_3ctry
keep digit_*
ren digit_3ctry country
mmerge country using `int',unmatched(u)
keep if _m==3
drop country
ren digit_2ctry country
drop _merge 
reshape long L_intr, i(country) j(year)
gen code =.
replace code =1 if L_intr != .
bys country: egen sum_code=sum(code)
keep if sum_code==15
drop code sum_code
drop if country == "IS" | country =="NO" |country == "NZ" | country == "ZA"
reshape wide L_intr, i(year) j(country, string)
save ${dataset_dir}/import/long-term_intr.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat